import {CSVManager} from "db://assets/scripts/common/CSVManager";
import {CSVPaths} from "db://assets/scripts/common/CSVPaths";
import {ChapterConverter} from "db://assets/scripts/data/converter/ChapterConverter";
import {Chapter} from "db://assets/scripts/data/model/Chapter";

/**
 * 章节管理器
 */
export class ChapterManager {
    /**
     * 章节信息
     */
    private chapters: Chapter[];

    /**
     * 章节管理器实例
     */
    private static instance: ChapterManager;

    /**
     * 获取章节管理器实例
     */
    public static getInstance(): ChapterManager {
        if (this.instance) {
            return this.instance;
        }
        this.instance = new ChapterManager();
        return this.instance;
    }

    private constructor() {
        // 初始化
        this.init();
    }

    /**
     * 初始化
     */
    private init() {
        let table = CSVManager.getInstance().getTable(CSVPaths.chapter);
        this.chapters = ChapterConverter.convertFromCSV(table);
    }

    /**
     * 获取指定层级的信息
     *
     * @param floor 层级
     */
    public getChapter(floor: number): Chapter {
        return this.chapters.find(chapter => chapter.id === String(floor));
    }

    /**
     * 获取章节列表
     */
    public getChapters(): Chapter[] {
        return this.chapters;
    }
}